/**
 * Created by fq on 2018/1/23 0023.
 */
'use strict';
angular.module('indexApp')
    .controller('loginCtrl', function ($scope, $state, $rootScope, $http) {
        var vm = this;
        vm.http = $rootScope.http;
        $scope.state = $state;
        vm.wei_xin = localStorage.getItem("wei_xin");
        if (vm.wei_xin != '' && vm.wei_xin != "wei_xin" && vm.wei_xin != null) {
            vm.wei_xin_news = JSON.parse(vm.wei_xin);
            vm.headImg=vm.wei_xin_news.headimgurl;
            vm.name=vm.wei_xin_news.nickname;
        }


//是否已经登陆
//         vm.person = JSON.parse(localStorage.getItem("login"));
//         if(JSON.parse(localStorage.getItem("login")).login == true){
//             $state.go('main.personal', {reload : true});
//         } else{
            //发送验证码
            vm.sendSms = function (num, num2) {
                $http.post(vm.http + '/user/login/sendSms?phone=' + num + '&type=' + num2)
                    .then(function successCallback(res) {
                        vm.sms = res.data.data;
                        if(res.data.code===1){
                            $rootScope.alert(res.data.msg)
                        }
                        else{
                            $rootScope.alert(res.data.msg)
                        }
                    }, function errorCallback(res) {// 请求失败执行代码
                        $rootScope.alert(res.data.msg)
                    });
            };
            // 登录
            vm.login = function (num1, num2) {
                $http.post(vm.http + '/user/login/doLogin?phone=' + num1 +'&code=' + num2)
                    .then(function successCallback(res) {
                        if(res.data.code===1){
                            vm.person = {
                                login : true,
                                id : res.data.id
                            };
                            //判断从返修页面过来的localStorage
                            vm.backurl = localStorage.getItem("backurl");
                            vm.backProduct=localStorage.getItem('backProduct');
                            vm.backGoodsDetail=localStorage.getItem('backGoodsDetail');
                            vm.backHome=localStorage.getItem('backHome');
                            vm.backShopCar=localStorage.getItem('backShopCar');
                            if(vm.backurl){
                                window.location.href=vm.backurl;
                                localStorage.removeItem('backurl')
                            }
                            else
                            if(vm.backProduct){
                                window.location.href=vm.backProduct;
                                localStorage.removeItem('backProduct')
                            }
                            else
                            if(vm.backGoodsDetail){
                                window.location.href=vm.backGoodsDetail;
                                localStorage.removeItem('backGoodsDetail')
                            }
                            else
                            if(vm.backHome){
                                $state.go('main.home');
                                localStorage.removeItem('backHome')
                            }
                            else
                            if(vm.backShopCar){
                                $state.go('main.shopCar');
                                localStorage.removeItem('backShopCar')
                            }
                            else{
                                //存储登录状态
                                localStorage.setItem("login", JSON.stringify(vm.person));
                                $state.go('main.personal')
                            }

                        }
                        else{
                            $rootScope.alert(res.data.msg)
                        }

                    }, function errorCallback(res) {// 请求失败执行代码
                        bootbox.alert({
                            title: '<p style="color: #47c6e8">提示</p>',
                            message: '请求失败，请稍后重试',
                            buttons: {
                                ok: {
                                    label: '确定',
                                    className: 'btn-primary'
                                }
                            }
                        });
                    });
            }
        // }



    });